package com.sample.projects.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONConverter;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.impl.Environment;
import com.sample.projects.Main;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 描述
 *
 * @author xlh
 * @date 2024/12/20
 * @desc
 */
@RequestMapping("/home")
@RestController
public class HomeController {

    @Value("${filePath}")
    private String filePath;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String sendMsg(@RequestParam(name = "orderNo", required = false) String orderNo) {
        HashMap<String, String> map = new HashMap<>();
        if(StringUtils.isNotBlank(orderNo)) {
            map.put("thirdOrderNo", orderNo);
//            rabbitTemplate.convertAndSend("hk_wzf_rerun_plan_exchange ", "hk_wzf_rerun_plan_queue", JSONUtil.toJsonStr(map));
            return "推送订单号：" + orderNo;
        }

        List<String> orders = new ArrayList<>();
        List<String> lines = FileUtil.readLines(filePath, Charset.forName("UTF-8"));
        for(String line : lines) {
            orders.add(line);
            map.put("thirdOrderNo", orderNo);
//            rabbitTemplate.convertAndSend("hk_wzf_rerun_plan_exchange ", "hk_wzf_rerun_plan_queue", JSONUtil.toJsonStr(map));
            map.clear();
        }
        return "推送订单号：" + String.join(",", orders);
    }

    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("thirdOrderNo", "orderNo");
        System.out.println(JSONUtil.toJsonStr(map));
    }
}
